package com.bolt.admin.module.quartz.service;

import com.bolt.common.utils.SpringContextUtil;
import com.bolt.common.utils.StrUtil;

import java.lang.reflect.Method;

/**
 * Created by Administrator on 2020/10/10.
 */
public class QuartzTask {
    private final Object target;
    private final Method method;
    private final String params;

    QuartzTask(String beanName, String methodName, String params)
            throws NoSuchMethodException, SecurityException {
        this.target = SpringContextUtil.getBean(beanName);
        this.params = params;

        if (StrUtil.isNotBlank(params)) {
            this.method = target.getClass().getDeclaredMethod(methodName, String.class);
        } else {
            this.method = target.getClass().getDeclaredMethod(methodName);
        }
    }


    public Object call() throws Exception {
        Object result = null;
        if (StrUtil.isNotBlank(params)) {
            result = method.invoke(target, params);
        } else {
            result = method.invoke(target);
        }
        return result;
    }
}

 